package com.canto.common.services;

import com.canto.common.dao.BaseDao;
import com.canto.common.pojo.doo.BaseDO;
import com.canto.common.pojo.entity.BaseEntity;
import com.canto.common.pojo.query.BaseQuery;
import com.canto.common.utils.IDUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 数据处理基类
 *
 * @param * <D> Doa层向上传输的查询结果对象
 *          <Q> Doa层接收的查询条件数据对象
 *          <E> Dao层接收的插入或修改的数据对象
 * @Author jsryin
 * @Date 2021/7/22 7:37
 */
@Transactional(readOnly = true)
public class DataService<T extends BaseDao> extends BaseService {

    @Autowired
    protected T dao;


    /**
     * 根据id查询
     *
     * @param query
     * @return
     */
    public <D extends BaseDO, Q extends BaseQuery> D get(Q query) {
        return dao.get(query);
    }

    /**
     * 通用查询列表数据方法
     *
     * @param query
     * @return
     */
    public <D extends BaseDO, Q extends BaseQuery> List<D> findList(Q query) {
        return dao.findList(query);
    }

    /**
     * 保存数据
     *
     * @param entity
     * @param <E>
     * @return
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public <E extends BaseEntity> long save(E entity) {
        long count = 0;
        if (StringUtils.isEmpty(entity.getId())) {
            //新增
            this.preInsert(entity);
            count = this.insert(entity);
        } else {
            //修改
            this.preUpdate(entity);
            count = this.update(entity);
        }
        return count;
    }

    /**
     * 添加数据前初始化操作
     *
     * @param entity
     * @param <E>
     */
    public <E extends BaseEntity> void preInsert(E entity) {
        entity.setId(IDUtils.nextSnowId());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(entity.getCreateTime());
        if (StringUtils.isEmpty(entity.getFlag())) entity.setFlag(BaseEntity.DEL_FLAG_NOT);
    }

    /**
     * 修改数据前初始化操作
     *
     * @param entity
     * @param <E>
     */
    public <E extends BaseEntity> void preUpdate(E entity) {
        entity.setUpdateTime(new Date());
        if (StringUtils.isEmpty(entity.getFlag())) entity.setFlag(BaseEntity.DEL_FLAG_NOT);
    }

    /**
     * 通用新增数据方法
     *
     * @param entity
     * @return
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public <E extends BaseEntity> long insert(E entity) {
        return dao.insert(entity);
    }

    /**
     * 通用修改方法
     *
     * @param entity
     * @return
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public <E extends BaseEntity> long update(E entity) {
        return dao.update(entity);
    }

    /**
     * 逻辑删除
     *
     * @param entity
     * @return
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public <E extends BaseEntity> long delete(E entity) {
        entity.setFlag(BaseEntity.DEL_FLAG_YES);
        return dao.delete(entity);
    }

}
